package com.hzdaba.aiapi.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 创建线程池执行任务
 * 我对线程池不熟悉，刚学，所以就是意思一下
 *
 * @author 刘逸晖
 */
@Configuration
public class ThreadPoolConfiguration {

    /**
     * 创建批量导入号码的线程池
     * 一些值应该放到配置文件里的
     *
     * @return 用于执行同步号码的线程池
     */
//    @Bean(name = "numberImportExecutorService", destroyMethod = "shutdown")
    @Bean(name = "${aiapi.ainumlist.import.thread.pool.name}", destroyMethod = "shutdown")
    public ExecutorService executorService() {
        ExecutorService executorService = new ThreadPoolExecutor(1,//核心县城数
                5,//最大线程数
                60L,//空闲县城存活时间
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(20),//队列
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()//拒绝策略
        );
        return executorService;
    }

    /**
     * 专门用于销毁上面的线程池
     * 多此一举
     *
     * @param executorService 预销毁的线程池
     * @return 销毁线程池的对象
     */
//    @Bean(destroyMethod = "destroyThreadPool")
    public ThreadPoolDestroyer threadPoolDestroyer(ExecutorService executorService) {
        ThreadPoolDestroyer threadPoolDestroyer = new ThreadPoolDestroyer();

        threadPoolDestroyer.setExecutorService(executorService);

        return threadPoolDestroyer;
    }

}
